<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="80px"
    >
<!--      <el-form-item label="openid" prop="openid">-->
<!--        <el-input-->
<!--          v-model="queryParams.openid"-->
<!--          placeholder="请输入openid"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-200px"-->
<!--        />-->
<!--      </el-form-item>-->

      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker
          v-model="queryParams.payTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-200px"
        />
      </el-form-item>



      <el-form-item label="姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-160px"
        />
      </el-form-item>
<!--      <el-form-item label="身份证号" prop="idCardNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.idCardNo"-->
<!--          placeholder="请输入身份证号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-200px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="病人ID" prop="patientId">
        <el-input
          v-model="queryParams.patientId"
          placeholder="请输入病人ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-160px"
        />
      </el-form-item>
      <el-form-item label="门诊号" prop="clinicNo">
        <el-input
          v-model="queryParams.clinicNo"
          placeholder="请输入门诊号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-160px"
        />
      </el-form-item>
<!--      <el-form-item label="电话" prop="phone">-->
<!--        <el-input-->
<!--          v-model="queryParams.phone"-->
<!--          placeholder="请输入电话"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-200px"-->
<!--        />-->
<!--      </el-form-item>-->

      <el-form-item label="订单类型" prop="orderType">
        <el-select
          v-model="queryParams.orderType"
          placeholder="请选择订单类型"
          clearable
          class="!w-220px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>

      <el-form-item label="HIS单据号" prop="hisBillNos">
        <el-input
          v-model="queryParams.hisBillNos"
          placeholder="请输入HIS单据号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-160px"
        />
      </el-form-item>
<!--      <el-form-item label="HIS订单号" prop="hisOrderNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.hisOrderNo"-->
<!--          placeholder="请输入HIS订单号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-200px"-->
<!--        />-->
<!--      </el-form-item>-->

      <el-form-item label="订单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入支付订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-220px"
        />
      </el-form-item>

<!--      <el-form-item label="渠道订单号" prop="thirdOrderNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.thirdOrderNo"-->
<!--          placeholder="请输入渠道订单号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--          class="!w-200px"-->
<!--        />-->
<!--      </el-form-item>-->


<!--      <el-form-item label="0 未支付 1 支付成功 2 已退款 3 申请退款" prop="payStatus">-->
<!--        <el-select-->
<!--          v-model="queryParams.payStatus"-->
<!--          placeholder="请选择0 未支付 1 支付成功 2 已退款 3 申请退款"-->
<!--          clearable-->
<!--          class="!w-200px"-->
<!--        >-->
<!--          <el-option label="请选择字典生成" value="" />-->
<!--        </el-select>-->
<!--      </el-form-item>-->


<!--      <el-form-item label="创建时间" prop="createTime">-->
<!--        <el-date-picker-->
<!--          v-model="queryParams.createTime"-->
<!--          value-format="YYYY-MM-DD HH:mm:ss"-->
<!--          type="daterange"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期"-->
<!--          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"-->
<!--          class="!w-200px"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          @click="openForm('create')"-->
<!--          v-hasPermi="['hospital:qrcode-pay:create']"-->
<!--        >-->
<!--          <Icon icon="ep:plus" class="mr-5px" /> 新增-->
<!--        </el-button>-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          @click="handleExport"-->
<!--          :loading="exportLoading"-->
<!--          v-hasPermi="['hospital:qrcode-pay:export']"-->
<!--        >-->
<!--          <Icon icon="ep:download" class="mr-5px" /> 导出-->
<!--        </el-button>-->
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" border :show-overflow-tooltip="true" :data="list" :row-class-name="tableRowClassName" >
<!--      <el-table-column label="openid" align="center" prop="openid" />-->
<!--      <el-table-column label="机构名称" align="center" prop="orgName" />-->
<!--      <el-table-column label="标题" align="center" prop="title"  min-width="150" fixed="left"/>-->

      <el-table-column label="姓名" align="center" prop="patientName" min-width="70" fixed="left"/>
      <el-table-column label="病人ID" align="center" prop="patientId"  min-width="70" fixed="left"/>

<!--      <el-table-column label="HIS订单号" align="center" prop="hisOrderNo" />-->
      <el-table-column label="订单号" align="center" prop="orderNumber"  min-width="195"/>
      <el-table-column label="HIS订单号" align="center" prop="hisOrderNo"  min-width="100"/>
      <el-table-column label="支付订单号" align="center" prop="payOrderId"   min-width="160"/>
<!--      <el-table-column label="付款码" align="center" prop="authCode" />-->
      <el-table-column label="金额" align="center" prop="amount"  min-width="70"/>
<!--      <el-table-column label="其他费用" align="center" prop="otherAmount" />-->
      <el-table-column label="支付状态" align="center" prop="payStatus" width="80" >
        <template #default="scope">
          <el-tag v-if="scope.row.payStatus === 0" type="info" >待支付</el-tag>
          <el-tag v-else-if="scope.row.payStatus === 1" type="primary" >支付中</el-tag>
          <el-tag v-else-if="scope.row.payStatus === 2" type="success" >支付成功</el-tag>
          <el-tag v-else-if="scope.row.payStatus === 3" type="danger" :show-overflow-tooltip="true">
            支付失败 {{ scope.row.remark }}
          </el-tag>
          <el-tag v-else-if="scope.row.payStatus === 4" type="warning" >已撤销</el-tag>
          <el-tag v-else-if="scope.row.payStatus === 5" type="warning" >已退款</el-tag>
          <el-tag v-else-if="scope.row.payStatus === 6" type="warning" >订单关闭</el-tag>
          <el-tag v-else-if="scope.row.payStatus === 7" type="warning" >部分退款</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="门诊号" align="center" prop="clinicNo"  min-width="100"/>
      <el-table-column label="身份证号" align="center" prop="idCardNo"  min-width="150"/>
      <!--      <el-table-column label="电话" align="center" prop="phone" />-->
      <el-table-column label="HIS单据号" align="center" prop="hisBillNos" min-width="100"/>
      <el-table-column label="渠道订单号" align="center" prop="thirdOrderNo"  min-width="220"/>
      <el-table-column label="支付类型ID" align="center" prop="payTypeId"   min-width="80"/>
      <el-table-column label="支付方式" align="center" prop="payModeName"   min-width="70"/>
      <el-table-column label="付款码" align="center" prop="authCode" min-width="150"/>
      <el-table-column label="用户" align="center" prop="userId" width="70"/>
      <el-table-column label="标题" align="center" prop="title"  min-width="150"/>
      <el-table-column label="订单类型" align="center" prop="orderType"  min-width="70"/>
      <el-table-column label="openid" align="center" prop="openid" width="230"/>

<!--      <el-table-column label="支付方式" align="center" prop="payType" />-->
      <el-table-column label="退款订单号" align="center" prop="refundNumber"  min-width="195"/>
      <el-table-column label="退款金额" align="center" prop="refundPrice"  min-width="70"/>
      <el-table-column
        label="退款时间"
        align="center"
        prop="refundTime"
        :formatter="dateFormatter"
        width="150"
      />
<!--      <el-table-column label="状态" align="center" prop="status" />-->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="140"
      />
      <el-table-column
        label="支付时间"
        align="center"
        prop="payTime"
        :formatter="dateFormatter"
        width="140"
        fixed="right"
      />
      <el-table-column label="操作" align="center" min-width="125" fixed="right">
        <template #default="scope">
          <el-button
            plain
            size="small"
            type="primary"
            @click="handleQueryStatus(scope.row.id)"
          >
            同步
          </el-button>

          <el-button
            plain
            size="small"
            type="danger"
            @click="openRefundForm(scope.row.id)"
            v-hasPermi="['hospital:qrcode-pay:refund']"
          >
            退款
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <QrcodePayForm ref="formRef" @success="getList" />
  <!-- 表单弹窗：退款 -->
  <QrcodePayRefundForm ref="refundRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { QrcodePayApi, QrcodePayVO } from '@/api/hospital/qrcodepay'
import QrcodePayForm from './QrcodePayForm.vue'
import QrcodePayRefundForm from './QrcodePayRefundForm.vue'

/** 扫码支付 列表 */
defineOptions({ name: 'QrcodePay' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<QrcodePayVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: undefined,
  openid: undefined,
  orgName: undefined,
  orgCode: undefined,
  patientName: undefined,
  idCardNo: undefined,
  patientId: undefined,
  clinicNo: undefined,
  phone: undefined,
  hisBillNos: undefined,
  hisOrderNo: undefined,
  orderType: undefined,
  orderNumber: undefined,
  authCode: undefined,
  thirdOrderNo: undefined,
  amount: undefined,
  otherAmount: undefined,
  payStatus: undefined,
  payTime: [],
  payType: undefined,
  refundNumber: undefined,
  refundPrice: undefined,
  refundTime: [],
  refundReason: undefined,
  remark: undefined,
  status: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await QrcodePayApi.getQrcodePayPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 退款操作 */
const refundRef = ref()
const openRefundForm = (id?: number) => {
  refundRef.value.open(id)
}


/** 查询支付状态按钮操作 */
const handleQueryStatus = async (id: number) => {
  loading.value = true
  try {
    const data = await QrcodePayApi.queryQrcodePayStatus(id)
    // 刷新列表
    await getList()
  } finally {
    loading.value = false
  }
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await QrcodePayApi.exportQrcodePay(queryParams)
    download.excel(data, '扫码支付.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

const tableRowClassName = ({row}) => {
  if (row.payStatus === 2) {
    return 'my-table success-row'
  } else if (row.payStatus === 3) {
    return 'my-table fails-row'
  } else if (row.payStatus === 5) {
    return 'my-table REFUND-row'
  }
  return 'my-table'
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>


<style scoped>
:deep(.el-table) {
  position: relative;
  overflow: hidden;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  -webkit-box-flex: 1;
  -ms-flex: 1;
  flex: 1;
  width: 100%;
  max-width: 100%;
  font-size: 12px;
  color: #000000;
}
:deep(.REFUND-row) {
  background-color: #F3D39BFF !important;
}

:deep(.fails-row) {
  background-color: #D15B47 !important;
}

:deep(.success-row) {
  background-color: #36bc99 !important;
}
:deep(.my-table td) {
  padding: 5px 0 !important;
}

</style>
